package com.bookstore.client.order.service;

import com.bookstore.client.order.dao.IOrderDao;
import com.bookstore.commons.beans.Order;
import com.bookstore.commons.beans.Orderitem;
import com.bookstore.commons.beans.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    IOrderDao orderDao;

    @Override
    public void createOrder(Order order, Map<Product, Integer> cart) {
        //orderitem表中   order_id订单id      product_id商品id      buynum商品数量
        for (Product p:cart.keySet()){    //将cart中的信息循环放到p中
            Orderitem item = new Orderitem();
            item.setProduct(p);
            item.setOrder(order);
            item.setBuynum(cart.get(p));
            orderDao.insertOrderItem(item);
            //修改商品库存
            orderDao.updateProductnum(item);
        }

//        if(1==1){      //测试事务
//            int a = 1/0;
//        }
        orderDao.insertOrder(order);
    }

    @Override
    public void paysuccess(String order_id) {
        orderDao.updatePaystate(order_id);
    }
}
